package com.ai.aicontact.common.util;

import org.apache.commons.lang.StringUtils;

import java.io.File;

public class FileUtil {

	/**
	 * 
	 * 生产文件 如果文件所在路径不存在则生成路径
	 * 
	 * 
	 * 
	 * @param fileName
	 * 
	 *            文件名 带路径
	 * 
	 * @param isDirectory
	 *            是否为路径
	 * 
	 * @return
	 * 
	 * @author yayagepei
	 * 
	 * @date 2008-8-27
	 */
	public static File buildFile(String fileName, boolean isDirectory) {

		File target = new File(fileName);

		if (isDirectory) {

			target.mkdirs();

		} else {

			if (!target.getParentFile().exists()) {

				target.getParentFile().mkdirs();

				target = new File(target.getAbsolutePath());

			}

		}

		return target;

	}

	public static boolean isFileInList(String[] fileList, String fileName) {
		if (StringUtils.isNotBlank(fileName) && fileList != null)
			for (String file : fileList) {
				if (StringUtils.equals(file, fileName)) {
					return true;
				}
			}
		return false;
	}

	public static boolean isFileInList(File[] fileList, String fileName) {
		if (StringUtils.isNotBlank(fileName) && fileList != null)
			for (File file : fileList) {
				if (StringUtils.equals(file.getName(), fileName)) {
					return true;
				}
			}
		return false;
	}

	public static File getFileInList(File[] fileList, String fileName) {
		if (StringUtils.isNotBlank(fileName) && fileList != null)
			for (File file : fileList) {
				if (StringUtils.equals(file.getName(), fileName)) {
					return file;
				}
			}
		return null;
	}

}
